package zkk.com.mengqu;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.base.library.application.BaseApplication;


import java.util.HashMap;
import java.util.Map;

import cn.smssdk.SMSSDK;
import zkk.com.mengqu.simplepreferences.Share;
import zkk.com.mengqu.util.FinalString;

public class App extends BaseApplication {
    private static App app;
    private static Context context;
    public SharedPreferences share;
    public LocationClient mLocationClient;//定位SDK的核心类
    public MyLocationListener mMyLocationListener;//定义监听类
    public TextView location;

    public RelativeLayout title;
    public RelativeLayout chooseSwitch;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        context = getApplicationContext();
//        initDownloader();

        share = Share.create(context);

        SMSSDK.initSDK(this, FinalString.AppKey, FinalString.AppSecret);

        //百度定位
        mLocationClient = new LocationClient(this.getApplicationContext());
        mMyLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(mMyLocationListener);
    }

    public static App app(){
        return app;
    }

//    private void initDownloader() {
//        DownloadConfiguration configuration = new DownloadConfiguration();
//        configuration.setMaxThreadNum(100);
//        configuration.setThreadNum(10);
//        DownloadManager.getInstance().init(getApplicationContext(), configuration);
//    }

    public static Context getContext() {
        return context;
    }

    /**
     * 实现实位回调监听
     */
    public class MyLocationListener implements BDLocationListener {

        public void onReceiveLocation(BDLocation location) {
            //Receive Location
            StringBuffer sb = new StringBuffer(256);
            sb.append("time : ");
            sb.append(location.getTime());//获得当前时间
            sb.append("\nerror code : ");
            sb.append(location.getLocType());//获得erro code得知定位现状
            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());//获得纬度
            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());//获得经度
            sb.append("\nradius : ");
            sb.append(location.getRadius());
            if (location.getLocType() == BDLocation.TypeGpsLocation){//通过GPS定位
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());//获得速度
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
                sb.append("\ndirection : ");
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());//获得当前地址
                sb.append(location.getDirection());//获得方位
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){//通过网络连接定位
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());//获得当前地址
                //运营商信息
                sb.append("\noperationers : ");
                sb.append(location.getOperators());//获得经营商？
            }
//            logMsg(sb.toString());
            logMsg(location.getStreet());
            Map<String,String> maps = new HashMap<>();
            maps.put(Share.LATITUDE, "" + location.getLatitude());
            maps.put(Share.LONGITUDE, "" + location.getLongitude());
            Share.setShare(maps);
            Log.i("BaiduLocationApiDem", sb.toString());
        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }
    }

    //给testView赋值
    public void logMsg(String str) {
        final String s = str;
        Log.w("address" , "" + str);
        try {
            if (location != null) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        location.post(new Runnable() {
                            @Override
                            public void run() {
                                location.setText(s);
                                mLocationClient.stop();
//                                Toast.makeText(context , R.string.location_successful ,Toast.LENGTH_LONG).show();
                            }
                        });

                    }
                }).start();
            }
            //LocationResult.setText(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //计算时间
    public String getTime(){
        long currentTime = System.currentTimeMillis() - 1495245205162l;
        int hour = 60 * 60 * 1000;
        int minute = 60 * 1000;

        if (currentTime / hour == 0){//小于一个小时
            if (currentTime < minute){
                return "刚刚";
            }else {
                int currentMinute = (int)currentTime / minute;;
                return currentMinute + "分钟前";
            }
        }else {// 大于一个小时
            int currentHour = (int)currentTime / hour;
            if (currentHour < 24){
                return currentHour + "小时前";
            }else {
                int currentDay = currentHour /24;
                return currentDay + "天前";
            }
        }
    }
}
